package com.zzmcc.jms.controller;

import com.jfinal.aop.Before;
import com.jfinal.ext.render.excel.JxlsRender;
import com.jfinal.kit.PathKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.zzmcc.base.BaseController;
import com.zzmcc.common.AuthorizeInterceptor;
import com.zzmcc.jms.model.Project;
import com.zzmcc.jms.model.ProjectAddress;
import com.zzmcc.jms.model.ProjectBuild;
import com.zzmcc.jms.model.ProjectEquip;
import com.zzmcc.sys.model.Userinfo;
import util.ChristStringUtil;
import util.ModelUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zzmcc.jms.common.ModelConfigWms.TABLE_Project;


public class ProjectAddressController extends BaseController {
    private static final Logger log =  Logger.getLogger(ProjectAddressController.class);
	@Before(AuthorizeInterceptor.class)

	public void index() {

        StringBuffer whee = new StringBuffer();
        List<Object> param = new ArrayList<Object>();
        String name = getPara("project.name");
		String code = getPara("project.code");
		String isReach = getPara("project.isReach");
		String share = getPara("project.share");
		String isSelectConfirm = getPara("project.isSelectConfirm");
		String demandOwner = getPara("project.demandOwner");
		String projectOwner = getPara("project.projectOwner");
        String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		String homeCounty = getPara("project.homeCounty");

        if(ChristStringUtil.isNotEmpty(name)){
            whee.append(" and name like ?");
            param.add("%" + name + "%");
        }
		if(ChristStringUtil.isNotEmpty(code)){
			whee.append(" and code like ?");
			param.add("%" + code + "%");
		}
		if(ChristStringUtil.isNotEmpty(demandOwner)){
			whee.append(" and demandOwner like ?");
			param.add("%" + demandOwner + "%");
		}

		if(ChristStringUtil.isNotEmpty(projectOwner)){
			whee.append(" and projectOwner like ?");
			param.add("%" + projectOwner + "%");
		}

		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and homeCounty = ?");
			param.add(homeCounty);
		}

		if(ChristStringUtil.isNotEmpty(isReach)){
			whee.append(" and coalesce(b.isReach,'否') = ?");
			param.add(isReach);
		}
		if(ChristStringUtil.isNotEmpty(share)){
			whee.append(" and share = ?");
			param.add(share);
		}


		Page<ProjectAddress> pager = ProjectAddress.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
				"select a.id,a.homeCounty,a.name,a.longitude,a.latitude,a.addr,a.share,a.projectDate,a.demandOwner,a.projectOwner,a.isProject,isCm,isCt,isCu," +
						"b.isReach,b.reachDate, b.addrType,c.addrDate,b.addrMemo ",
				" from jms_project a left join jms_project_build b on b.pid=a.pid " +
				" where a.isSelectConfirm='是' "+ whee.toString()+" order by b.modifyDate desc",
				param.toArray());
		setAttr("pager",pager);
		setAttr("name",name);
		setAttr("demandOwner",demandOwner);
		setAttr("projectOwner",projectOwner);
		setAttr("share",share);
		setAttr("isSelectConfirm",isSelectConfirm);
		setAttr("isReach",isReach);
		setAttr("endDate",endDate);
		setAttr("homeCounty",homeCounty);
		render("../project_address_list.html");

	}

	public void add() {

		String id = getPara("id");
		Userinfo loginUser =getSessionAttr("loginUser");
		String modifyUser=loginUser.get("realname");
		int groupid=loginUser.get("groupid");

		String sql="select a.name,a.pid,a.code," +
				"b.id, b.addressType,b.createDate,b.modifyMemo" +
				" from jms_project a left join jms_project_address b on b.pid=a.pid " +
				" where a.id=?";
		ProjectAddress m = ProjectAddress.dao.findFirst(sql, id);
		setAttr("ProjectBuild",m);
		setAttr("modifyUser", modifyUser);
		setAttr("groupid",groupid);

		render("../project_address_input.html");
	}
	
	public void update() {
		ProjectBuild m = getModel(ProjectBuild.class, "ProjectBuild");

		ModelUtils.fillForUpdate(m);
		m.update();
		renderDWZSuccessJson("修改成功！");
	}

	public void save() {
		try{
			ProjectBuild m = getModel(ProjectBuild.class, "ProjectBuild");
			ModelUtils.fillForSave(m);
			m.save();
			renderDWZSuccessJson("添加成功！");
		}catch (Exception e){
			renderDWZSuccessJson("重复提交，失败！");
		}
	}


	public void export(){

		//导出条件
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();

		String homeCounty = getPara("project.homeCounty");
		String owner = getPara("project.owner");
		String isCm = getPara("project.isCm");
		String isCu = getPara("project.isCu");
		String isCt = getPara("project.isCt");
		String modifyDate = getPara("project.modifyDate");


		if(ChristStringUtil.isNotEmpty(owner)){
			whee.append(" and owner like ?");
			param.add("%" + owner + "%");
		}
		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and homeCounty = ?");
			param.add(homeCounty);
		}

		if(ChristStringUtil.isNotEmpty(isCm)){
			whee.append(" and isCm = ?");
			param.add(isCm);
		}
		if(ChristStringUtil.isNotEmpty(isCt)){
			whee.append(" and isCt = ?");
			param.add(isCt);
		}
		if(ChristStringUtil.isNotEmpty(isCu)){
			whee.append(" and isCu = ?");
			param.add(isCu);
		}
		if(ChristStringUtil.isNotEmpty(modifyDate)){
			whee.append(" and TO_DAYS(modifyDate) >= TO_DAYS(?)");
			param.add( modifyDate );
		}


		String sql="select a.id,a.homeCounty,a.name,a.longitude,a.latitude,a.addr,a.share,a.projectDate,a.demandOwner,a.projectOwner,a.isProject,isCm,isCt,isCu," +
				"b.isReach,b.reachDate, c.addressType,c.createDate,c.modifyDate,c.modifyMemo "+
				" from jms_project a left join jms_project_build b on b.pid=a.pid " +
				"left join jms_project_address c on a.pid=c.pid \n"+
				" where a.isSelectConfirm='是' "+ whee.toString()+" order by c.modifyDate desc";

		List<Record> projectAddress = Db.find(sql, param.toArray());

		Map<String, Object> beans = new HashMap<String, Object>();
		beans.put("projectAddress", projectAddress);

		String templateFileName = PathKit.getRootClassPath() + "/tpl_project_address.xls";
		String filename = "project_address.xls";
		render(JxlsRender.me(templateFileName).filename(filename).beans(beans));
	}

	@Before(AuthorizeInterceptor.class)
	public void delete() {

		String[] ids = getParaValues("ids");
		for(String id:ids){
			Project m =Project.dao.findById(id);
			Db.update("delete a from jms_project_address a where pid =?", m.get("pid"));
		}
		renderDWZSuccessJson("删除成功!");

	}


}
